package com.yndsoft.jc.supplier.hr;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.ContactModel;
import com.yndsoft.jc.supplier.model.EntryModel;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.model.HolidayModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.RegexUtil;
import com.yndsoft.jc.supplier.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class AddUpdateActivity extends SupplierBaseActivity implements View.OnClickListener {

    private TextView auTitle1;
    private TextView auValue1;
    private TextView auTitle2;
    private TextView auValue2;
    private TextView auTitle3;
    private TextView auValue3;
    private String tag;
    private String function;
    private TextView barTitle;
    private String id;

    @Override
    public int getContentResId() {
        return R.layout.activity_add_update;
    }

    public static void start(Context context, String tag, String function) {
        start(context, tag, function, null, null, null, null);
    }

    public static void start(Context context, String tag, String function, String id, String value1, String value2, String value3) {
        Intent intent = new Intent(context, AddUpdateActivity.class);
        intent.putExtra(Constants.TAG, tag);
        intent.putExtra(Constants.FUNCTION, function);
        intent.putExtra(Constants.ID, id);
        intent.putExtra(Constants.VALUE1, value1);
        intent.putExtra(Constants.VALUE2, value2);
        intent.putExtra(Constants.VALUE3, value3);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        barTitle = findViewById(R.id.bar_title);
        auTitle1 = findViewById(R.id.au_title_1);
        auValue1 = findViewById(R.id.au_value_1);
        auTitle2 = findViewById(R.id.au_title_2);
        auValue2 = findViewById(R.id.au_value_2);
        auTitle3 = findViewById(R.id.au_title_3);
        auValue3 = findViewById(R.id.au_value_3);
        Button auSave = findViewById(R.id.au_save);

        barBack.setOnClickListener(this);
        auSave.setOnClickListener(this);
    }

    @Override
    public void initData() {

        tag = getIntent().getStringExtra(Constants.TAG);
        function = getIntent().getStringExtra(Constants.FUNCTION);
        id = getIntent().getStringExtra(Constants.ID);
        String value1 = getIntent().getStringExtra(Constants.VALUE1);
        String value2 = getIntent().getStringExtra(Constants.VALUE2);
        String value3 = getIntent().getStringExtra(Constants.VALUE3);
        if (tag != null && function != null) {
            setView(tag, function, value1, value2, value3);
        }

    }

    private void setView(String tag, String function, String value1, String value2, String value3) {
        if (function != null && function.equals(Constants.UPDATE)) {
            if (value1 != null) {
                auValue1.setText(value1);
            }
            if (value2 != null) {
                auValue2.setText(value2);
            }
            if (value3 != null) {
                auValue3.setText(value3);
            }
        }
        switch (tag) {
            case Constants.HOLIDAY:
                barTitle.setText("新增假期");
                auTitle1.setText("开始时间");
                auValue1.setHint("请输入时间");
                auTitle2.setText("结束时间");
                auValue2.setHint("请输入时间");
                auTitle3.setText("备注");
                auValue3.setHint("请输入说明");
                auValue3.setTransformationMethod(null);
                break;
            case Constants.CONTACT:
                setFunction(function, "联系人");
                auTitle1.setText("姓名");
                auValue1.setHint("请输入姓名");
                auTitle2.setText("手机");
                auValue2.setHint("请输入手机号");
                auTitle3.setText("邮箱");
                auValue3.setHint("请输入邮箱");
                auValue3.setTransformationMethod(null);
                break;
            case Constants.ENTRY:
                setFunction(function, "录入员");
                auTitle1.setText("手机");
                auValue1.setHint("请输入手机号");
                auTitle2.setText("昵称");
                auValue2.setHint("请输入昵称");
                auTitle3.setText("密码");
                auValue3.setHint("请输入密码");
                auValue3.setTransformationMethod(PasswordTransformationMethod.getInstance());
                break;
            case Constants.SIGN:
                setFunction(function, "审核员");
                auTitle1.setText("手机");
                auValue1.setHint("请输入手机号");
                auTitle2.setText("昵称");
                auValue2.setHint("请输入昵称");
                auTitle3.setText("密码");
                auValue3.setHint("请输入密码");
                auValue3.setTransformationMethod(PasswordTransformationMethod.getInstance());
                break;
        }
    }

    private void setFunction(String function, String title) {
        switch (function) {
            case Constants.ADD:
                barTitle.setText("新增" + title);
                break;
            case Constants.UPDATE:
                barTitle.setText("编辑" + title);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.au_save:
                if (tag != null) {
                    String content1 = auValue1.getText().toString();
                    String content2 = auValue2.getText().toString();
                    String content3 = auValue3.getText().toString();
                    switch (tag) {
                        case Constants.HOLIDAY:
                            if (TextUtils.isEmpty(content1)) {
                                showHint("请输入开始时间！");
                            } else {
                                if (TextUtils.isEmpty(content2)) {
                                    showHint("请输入结束时间！");
                                } else {
                                    if (TextUtils.isEmpty(content3)) {
                                        showHint("请输入备注！");
                                    } else {
                                        HolidayModel model = new HolidayModel();
                                        model.setSid(mSession.getSid());
                                        model.setStartdate(content1);
                                        model.setEnddate(content2);
                                        model.setNote(content3);
                                        String json = new Gson().toJson(model);
                                        saveData("addholiday", json);
                                    }
                                }
                            }
                            break;
                        case Constants.CONTACT:
                            if (TextUtils.isEmpty(content1)) {
                                showHint("请输入姓名！");
                            } else {
                                if (TextUtils.isEmpty(content2)) {
                                    showHint("请输入手机号！");
                                } else {
                                    if (content2.length() != 11) {
                                        showHint("请输入正确的手机号！");
                                    } else {
                                        if (TextUtils.isEmpty(content3)) {
                                            showHint("请输入邮箱！");
                                        } else {
                                            if (!RegexUtil.isEmail(content3)) {
                                                showHint("请输入正确的邮箱！");
                                            } else {
                                                ContactModel model = new ContactModel();
                                                if (function != null && function.equals(Constants.UPDATE)) {
                                                    model.setId(id);
                                                }
                                                model.setSid(mSession.getSid());
                                                model.setName(content1);
                                                model.setPhone(content2);
                                                model.setEmail(content3);
                                                String json = new Gson().toJson(model);
                                                if (function != null) {
                                                    switch (function) {
                                                        case Constants.ADD:
                                                            saveData("addcontact", json);
                                                            break;
                                                        case Constants.UPDATE:
                                                            saveData("editcontact", json);
                                                            break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        case Constants.ENTRY:
                        case Constants.SIGN:
                            if (TextUtils.isEmpty(content1)) {
                                showHint("请输入手机号！");
                            } else {
                                if (content1.length() != 11) {
                                    showHint("请输入正确的手机号！");
                                } else {
                                    if (TextUtils.isEmpty(content2)) {
                                        showHint("请输入昵称！");
                                    } else {
                                        if (TextUtils.isEmpty(content3)) {
                                            showHint("请输入密码！");
                                        } else {
                                            if (content3.length() < 6 || content3.length() > 20) {
                                                showHint("请输入6-20位密码！");
                                            } else {
                                                EntryModel model = new EntryModel();
                                                if (function != null && function.equals(Constants.UPDATE)) {
                                                    model.setId(id);
                                                }
                                                model.setSid(mSession.getSid());
                                                switch (tag) {
                                                    case Constants.ENTRY:
                                                        model.setTid("2");
                                                        break;
                                                    case Constants.SIGN:
                                                        model.setTid("3");
                                                        break;
                                                }
                                                model.setPhone(content1);
                                                model.setName(content2);
                                                model.setPwd(content3);
                                                String json = new Gson().toJson(model);
                                                if (function != null) {
                                                    switch (function) {
                                                        case Constants.ADD:
                                                            saveData("addadmin", json);
                                                            break;
                                                        case Constants.UPDATE:
                                                            saveData("editadmin", json);
                                                            break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
                break;
        }
    }

    private void saveData(String type, String json) {
        mHttpUtil.put(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                if (1 == returnMsg.getCode()) {
                    EventModel model = new EventModel();
                    model.setInteger(Constants.HR_ADD);
                    EventBus.getDefault().post(model);
                    ToastUtil.show(mContext, "保存成功！");
                    finish();
                } else if (2 == returnMsg.getCode()) {
                    showHint(returnMsg.getInfo() != null ? returnMsg.getInfo() : "保存失败！");
                } else {
                    ToastUtil.show(mContext, "保存失败！");
                }
            }
        }, type, json);
    }
}
